package com.bangdream.michelia.view.fragment.currency;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bangdream.michelia.R;
import com.bangdream.michelia.application.MainApplication;
import com.bangdream.michelia.presenter.base.BasePresenter;
import com.bangdream.michelia.sql.greendao.DbHelper;
import com.bangdream.michelia.sql.greendao.GreenDaoManager;
import com.bangdream.michelia.utils.L;

import io.reactivex.disposables.Disposable;

/**
 * @date on 2018/8/11 11:59
 * @email 909732184@qq.com
 * 懒加载
 */
public abstract class LazyLoadFragment<V,P extends BasePresenter<V>> extends BaseFragment<V,P> {
    public abstract P createPresenter();
    //第一次页面显示了才去拉取数据
    public abstract void loadData();
    private boolean isFirstLoad = true;
    private boolean isCreatr = false;
    public Disposable disposable;

    @Override
    public void onStart() {
        super.onStart();
        setUserVisibleHint(getUserVisibleHint());
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        isCreatr = true;
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser&&isFirstLoad&&isCreatr) {
            L.d("setUserVisibleHint");
            isFirstLoad = false;
            initConfig();
            loadData(); // 在此请求数据
        }
    }

    public View noDataView;
    public View errorView;
    public View loadingView;
    public TextView errText;

    public void initEmptyView(RecyclerView recyclerView){
        noDataView = LayoutInflater.from(recyclerView.getContext()).inflate(R.layout.empty_view, (ViewGroup) recyclerView.getParent(), false);
        errorView = LayoutInflater.from(recyclerView.getContext()).inflate(R.layout.error_view, (ViewGroup) recyclerView.getParent(), false);
        loadingView = LayoutInflater.from(recyclerView.getContext()).inflate(R.layout.loading_view, (ViewGroup) recyclerView.getParent(), false);
        errText = errorView.findViewById(R.id.errText);
    }


}
